{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "aaaaaaaaaa"
   },
   "source": [
    "Git clone the repo and install the requirements. (ignore the pip errors about protobuf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-03-21T08:53:59.697985Z",
     "iopub.status.busy": "2024-03-21T08:53:59.697679Z",
     "iopub.status.idle": "2024-03-21T08:54:04.511198Z",
     "shell.execute_reply": "2024-03-21T08:54:04.510652Z",
     "shell.execute_reply.started": "2024-03-21T08:53:59.697967Z"
    },
    "id": "bbbbbbbbbb",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/mnt/workspace\n",
      "-= Initial setup ComfyUI =-\n",
      "正克隆到 'ComfyUI'...\n",
      "remote: Enumerating objects: 10437, done.\u001b[K\n",
      "remote: Counting objects: 100% (79/79), done.\u001b[K\n",
      "remote: Compressing objects: 100% (50/50), done.\u001b[K\n",
      "remote: Total 10437 (delta 38), reused 38 (delta 29), pack-reused 10358\u001b[K\n",
      "接收对象中: 100% (10437/10437), 4.16 MiB | 2.80 MiB/s, 完成.\n",
      "处理 delta 中: 100% (7020/7020), 完成.\n",
      "/mnt/workspace/ComfyUI\n",
      "-= Updating ComfyUI =-\n",
      "已经是最新的。\n",
      "-= Install dependencies =-\n"
     ]
    }
   ],
   "source": [
    "#@title Environment Setup\n",
    "\n",
    "from pathlib import Path\n",
    "\n",
    "OPTIONS = {}\n",
    "\n",
    "UPDATE_COMFY_UI = True  #@param {type:\"boolean\"}\n",
    "WORKSPACE = 'ComfyUI'\n",
    "OPTIONS['UPDATE_COMFY_UI'] = UPDATE_COMFY_UI\n",
    "\n",
    "WORKSPACE = \"/mnt/workspace/ComfyUI\"\n",
    "%cd /mnt/workspace/\n",
    "\n",
    "![ ! -d $WORKSPACE ] && echo -= Initial setup ComfyUI =- && git clone https://github.com/comfyanonymous/ComfyUI\n",
    "%cd $WORKSPACE\n",
    "\n",
    "if OPTIONS['UPDATE_COMFY_UI']:\n",
    "  !echo -= Updating ComfyUI =-\n",
    "  !git pull\n",
    "\n",
    "!echo -= Install dependencies =-"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "cccccccccc"
   },
   "source": [
    "Download some models/checkpoints/vae or custom comfyui nodes (uncomment the commands for the ones you want)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-03-21T08:54:07.742825Z",
     "iopub.status.busy": "2024-03-21T08:54:07.742246Z",
     "iopub.status.idle": "2024-03-21T08:54:33.950314Z",
     "shell.execute_reply": "2024-03-21T08:54:33.949669Z",
     "shell.execute_reply.started": "2024-03-21T08:54:07.742798Z"
    },
    "id": "dddddddddd",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2024-03-21 16:54:07--  https://modelscope.cn/api/v1/models/AI-ModelScope/anything-v3.0/repo?Revision=master&FilePath=Anything-V3.0-pruned-fp16.safetensors\n",
      "正在解析主机 modelscope.cn (modelscope.cn)... 10.224.145.247, 10.224.140.111, 10.224.132.175, ...\n",
      "正在连接 modelscope.cn (modelscope.cn)|10.224.145.247|:443... 已连接。\n",
      "已发出 HTTP 请求，正在等待回应... 200 OK\n",
      "长度： 2132625640 (2.0G) [application/octet-stream]\n",
      "正在保存至: ‘./models/checkpoints/repo?Revision=master&FilePath=Anything-V3.0-pruned-fp16.safetensors’\n",
      "\n",
      "repo?Revision=maste 100%[===================>]   1.99G   122MB/s    用时 18s     \n",
      "\n",
      "2024-03-21 16:54:26 (111 MB/s) - 已保存 ‘./models/checkpoints/repo?Revision=master&FilePath=Anything-V3.0-pruned-fp16.safetensors’ [2132625640/2132625640])\n",
      "\n",
      "--2024-03-21 16:54:26--  https://modelscope.cn/api/v1/models/AI-ModelScope/sd-vae-ft-mse-original/repo?Revision=master&FilePath=vae-ft-mse-840000-ema-pruned.safetensors\n",
      "正在解析主机 modelscope.cn (modelscope.cn)... 10.224.145.247, 10.224.140.111, 10.224.132.175, ...\n",
      "正在连接 modelscope.cn (modelscope.cn)|10.224.145.247|:443... 已连接。\n",
      "已发出 HTTP 请求，正在等待回应... 200 OK\n",
      "长度： 334641190 (319M) [application/octet-stream]\n",
      "正在保存至: ‘./models/vae/repo?Revision=master&FilePath=vae-ft-mse-840000-ema-pruned.safetensors’\n",
      "\n",
      "repo?Revision=maste 100%[===================>] 319.14M   157MB/s    用时 2.0s    \n",
      "\n",
      "2024-03-21 16:54:29 (157 MB/s) - 已保存 ‘./models/vae/repo?Revision=master&FilePath=vae-ft-mse-840000-ema-pruned.safetensors’ [334641190/334641190])\n",
      "\n",
      "--2024-03-21 16:54:29--  https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_scribble_fp16.safetensors\n",
      "正在解析主机 modelscope.cn (modelscope.cn)... 10.224.132.175, 10.224.160.117, 10.224.145.247, ...\n",
      "正在连接 modelscope.cn (modelscope.cn)|10.224.132.175|:443... 已连接。\n",
      "已发出 HTTP 请求，正在等待回应... 200 OK\n",
      "长度： 722601100 (689M) [application/octet-stream]\n",
      "正在保存至: ‘./models/controlnet/repo?Revision=master&FilePath=control_v11p_sd15_scribble_fp16.safetensors’\n",
      "\n",
      "repo?Revision=maste 100%[===================>] 689.13M   172MB/s    用时 3.8s    \n",
      "\n",
      "2024-03-21 16:54:33 (180 MB/s) - 已保存 ‘./models/controlnet/repo?Revision=master&FilePath=control_v11p_sd15_scribble_fp16.safetensors’ [722601100/722601100])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Checkpoints\n",
    "\n",
    "### SDXL\n",
    "### I recommend these workflow examples: https://comfyanonymous.github.io/ComfyUI_examples/sdxl/\n",
    "\n",
    "#!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/stable-diffusion-xl-base-1.0/repo?Revision=master&FilePath=sd_xl_base_1.0.safetensors\" -P ./models/checkpoints/\n",
    "#!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/stable-diffusion-xl-refiner-1.0/repo?Revision=master&FilePath=sd_xl_refiner_1.0.safetensors\" -P ./models/checkpoints/\n",
    "\n",
    "# SDXL ReVision\n",
    "#!wget -c https://huggingface.co/comfyanonymous/clip_vision_g/resolve/main/clip_vision_g.safetensors -P ./models/clip_vision/\n",
    "\n",
    "# SD1.5\n",
    "#!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/stable-diffusion-v1-5/repo?Revision=master&FilePath=v1-5-pruned-emaonly.ckpt\" -P ./models/checkpoints/\n",
    "\n",
    "# SD2\n",
    "#!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/stable-diffusion-2-1-base/repo?Revision=master&FilePath=v2-1_512-ema-pruned.safetensors\" -P ./models/checkpoints/\n",
    "#!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/stable-diffusion-2-1/repo?Revision=master&FilePath=v2-1_768-ema-pruned.safetensors\" -P ./models/checkpoints/\n",
    "\n",
    "# Some SD1.5 anime style\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/Orange-Mixs/repo?Revision=master&FilePath=Models%2FAbyssOrangeMix2%2FAbyssOrangeMix2_hard.safetensors -P ./models/checkpoints/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/Orange-Mixs/repo?Revision=master&FilePath=Models%2FAbyssOrangeMix3%2FAOM3A1_orangemixs.safetensors -P ./models/checkpoints/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/Orange-Mixs/repo?Revision=master&FilePath=Models%2FAbyssOrangeMix3%2FAOM3A3_orangemixs.safetensors -P ./models/checkpoints/\n",
    "!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/anything-v3.0/repo?Revision=master&FilePath=Anything-V3.0-pruned-fp16.safetensors\" -P ./models/checkpoints/\n",
    "\n",
    "# Waifu Diffusion 1.5 (anime style SD2.x 768-v)\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/wd-1-5-beta3/repo?Revision=master&FilePath=wd-illusion-fp16.safetensors -P ./models/checkpoints/\n",
    "\n",
    "\n",
    "# unCLIP models\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/illuminatiDiffusionV1_v11_unCLIP/repo?Revision=master&FilePath=illuminatiDiffusionV1_v11-unclip-h-fp16.safetensors -P ./models/checkpoints/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/wd-1.5-beta2_unCLIP/repo?Revision=master&FilePath=wd-1-5-beta2-aesthetic-unclip-h-fp16.safetensors -P ./models/checkpoints/\n",
    "\n",
    "\n",
    "# VAE\n",
    "!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/sd-vae-ft-mse-original/repo?Revision=master&FilePath=vae-ft-mse-840000-ema-pruned.safetensors\" -P ./models/vae/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/Orange-Mixs/repo?Revision=master&FilePath=VAEs%2Forangemix.vae.pt -P ./models/vae/\n",
    "#!wget -c https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/main/vae/kl-f8-anime2.ckpt -P ./models/vae/\n",
    "\n",
    "\n",
    "# Loras\n",
    "#!wget -c https://civitai.com/api/download/models/10350 -O ./models/loras/theovercomer8sContrastFix_sd21768.safetensors #theovercomer8sContrastFix SD2.x 768-v\n",
    "#!wget -c https://civitai.com/api/download/models/10638 -O ./models/loras/theovercomer8sContrastFix_sd15.safetensors #theovercomer8sContrastFix SD1.x\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/stable-diffusion-xl-base-1.0/repo?Revision=master&FilePath=sd_xl_offset_example-lora_1.0.safetensors -P ./models/loras/ #SDXL offset noise lora\n",
    "\n",
    "\n",
    "# T2I-Adapter\n",
    "#!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_depth_sd14v1.pth -P ./models/controlnet/\"\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2F -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_sketch_sd14v1.pth -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_keypose_sd14v1.pth -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_openpose_sd14v1.pth -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_color_sd14v1.pth -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_canny_sd14v1.pth -P ./models/controlnet/\n",
    "\n",
    "# T2I Styles Model\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/T2I-Adapter/repo?Revision=master&FilePath=models%2Ft2iadapter_style_sd14v1.pth -P ./models/style_models/\n",
    "\n",
    "# CLIPVision model (needed for styles model)\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/clip-vit-large-patch14/repo?Revision=master&FilePath=pytorch_model.bin -O ./models/clip_vision/clip_vit14.bin\n",
    "\n",
    "\n",
    "# ControlNet\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11e_sd15_ip2p_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11e_sd15_shuffle_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_canny_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11f1p_sd15_depth_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_inpaint_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_lineart_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_mlsd_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_normalbae_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_openpose_fp16.safetensors -P ./models/controlnet/\n",
    "!wget -c \"https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_scribble_fp16.safetensors\" -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_seg_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15_softedge_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11p_sd15s2_lineart_anime_fp16.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/ControlNet-v1-1_fp16_safetensors/repo?Revision=master&FilePath=control_v11u_sd15_tile_fp16.safetensors -P ./models/controlnet/\n",
    "\n",
    "# ControlNet SDXL\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/control-lora/repo?Revision=master&FilePath=control-LoRAs-rank256%2Fcontrol-lora-canny-rank256.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/control-lora/repo?Revision=master&FilePath=control-LoRAs-rank256%2Fcontrol-lora-depth-rank256.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/control-lora/repo?Revision=master&FilePath=control-LoRAs-rank256%2Fcontrol-lora-recolor-rank256.safetensors -P ./models/controlnet/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/control-lora/repo?Revision=master&FilePath=control-LoRAs-rank256%2Fcontrol-lora-sketch-rank256.safetensors -P ./models/controlnet/\n",
    "\n",
    "# Controlnet Preprocessor nodes by Fannovel16\n",
    "#!cd custom_nodes && git clone https://github.com/Fannovel16/comfy_controlnet_preprocessors; cd comfy_controlnet_preprocessors && python install.py\n",
    "\n",
    "\n",
    "# GLIGEN\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/GLIGEN_pruned_safetensors/repo?Revision=master&FilePath=gligen_sd14_textbox_pruned_fp16.safetensors -P ./models/gligen/\n",
    "\n",
    "\n",
    "# ESRGAN upscale model\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/RealESRGAN_x4plus/repo?Revision=master&FilePath=RealESRGAN_x4plus.pth -P ./models/upscale_models/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/Real-ESRGAN/repo?Revision=master&FilePath=RealESRGAN_x2.pth -P ./models/upscale_models/\n",
    "#!wget -c https://modelscope.cn/api/v1/models/AI-ModelScope/Real-ESRGAN/repo?Revision=master&FilePath=RealESRGAN_x.pth -P ./models/upscale_models/\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "kkkkkkkkkkkkkkk"
   },
   "source": [
    "### Run ComfyUI with cloudflared (Recommended Way)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-03-21T08:54:36.597415Z",
     "iopub.status.busy": "2024-03-21T08:54:36.597047Z"
    },
    "id": "jjjjjjjjjjjjjj",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2024-03-21 16:54:36--  https://modelscope.oss-cn-beijing.aliyuncs.com/resource/cloudflared-linux-amd64.deb\n",
      "正在解析主机 modelscope.oss-cn-beijing.aliyuncs.com (modelscope.oss-cn-beijing.aliyuncs.com)... 8.131.208.119\n",
      "正在连接 modelscope.oss-cn-beijing.aliyuncs.com (modelscope.oss-cn-beijing.aliyuncs.com)|8.131.208.119|:443... 已连接。\n",
      "已发出 HTTP 请求，正在等待回应... 200 OK\n",
      "长度： 17717280 (17M) [application/octet-stream]\n",
      "正在保存至: ‘cloudflared-linux-amd64.deb’\n",
      "\n",
      "cloudflared-linux-a 100%[===================>]  16.90M  49.9MB/s    用时 0.3s    \n",
      "\n",
      "2024-03-21 16:54:37 (49.9 MB/s) - 已保存 ‘cloudflared-linux-amd64.deb’ [17717280/17717280])\n",
      "\n",
      "正在选中未选择的软件包 cloudflared。\n",
      "(正在读取数据库 ... 系统当前共安装有 79140 个文件和目录。)\n",
      "准备解压 cloudflared-linux-amd64.deb  ...\n",
      "正在解压 cloudflared (2023.10.0) ...\n",
      "正在设置 cloudflared (2023.10.0) ...\n",
      "Total VRAM 22732 MB, total RAM 29893 MB\n",
      "xformers version: 0.0.23.post1\n",
      "Set vram state to: NORMAL_VRAM\n",
      "Device: cuda:0 NVIDIA A10 : cudaMallocAsync\n",
      "VAE dtype: torch.bfloat16\n",
      "Using xformers cross attention\n",
      "****** User settings have been changed to be stored on the server instead of browser storage. ******\n",
      "****** For multi-user setups add the --multi-user CLI argument to enable multiple user profiles. ******\n",
      "\n",
      "ComfyUI finished loading, trying to launch cloudflared (if it gets stuck here cloudflared is having issues)\n",
      "\n",
      "This is the URL to access ComfyUI: https://glory-dv-davis-doc.trycloudflare.com                                              |\n"
     ]
    }
   ],
   "source": [
    "!wget \"https://modelscope.oss-cn-beijing.aliyuncs.com/resource/cloudflared-linux-amd64.deb\"\n",
    "!dpkg -i cloudflared-linux-amd64.deb\n",
    "\n",
    "import subprocess\n",
    "import threading\n",
    "import time\n",
    "import socket\n",
    "import urllib.request\n",
    "\n",
    "def iframe_thread(port):\n",
    "  while True:\n",
    "      time.sleep(0.5)\n",
    "      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "      result = sock.connect_ex(('127.0.0.1', port))\n",
    "      if result == 0:\n",
    "        break\n",
    "      sock.close()\n",
    "  print(\"\\nComfyUI finished loading, trying to launch cloudflared (if it gets stuck here cloudflared is having issues)\\n\")\n",
    "\n",
    "  p = subprocess.Popen([\"cloudflared\", \"tunnel\", \"--url\", \"http://127.0.0.1:{}\".format(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
    "  for line in p.stderr:\n",
    "    l = line.decode()\n",
    "    if \"trycloudflare.com \" in l:\n",
    "      print(\"This is the URL to access ComfyUI:\", l[l.find(\"http\"):], end='')\n",
    "    #print(l, end='')\n",
    "\n",
    "\n",
    "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n",
    "\n",
    "!python main.py --dont-print-server"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "kkkkkkkkkkkkkk"
   },
   "source": [
    "### Run ComfyUI with localtunnel\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-03-20T07:55:34.777470Z",
     "iopub.status.busy": "2024-03-20T07:55:34.777140Z",
     "iopub.status.idle": "2024-03-20T07:57:08.532198Z",
     "shell.execute_reply": "2024-03-20T07:57:08.531669Z",
     "shell.execute_reply.started": "2024-03-20T07:55:34.777450Z"
    },
    "id": "jjjjjjjjjjjjj",
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[K\u001b[?25h/etc/dsw/node/bin/lt -> /etc/dsw/node/lib/node_modules/localtunnel/bin/lt.js yargs@17.1.1 is installed in\u001b[0m\u001b[Km\u001b[K\n",
      "+ localtunnel@2.0.2\n",
      "added 22 packages from 22 contributors in 11.176s\n",
      "/mnt/workspace/ComfyUI\n",
      "Total VRAM 22732 MB, total RAM 29863 MB\n",
      "xformers version: 0.0.23.post1\n",
      "Set vram state to: NORMAL_VRAM\n",
      "Device: cuda:0 NVIDIA A10 : cudaMallocAsync\n",
      "VAE dtype: torch.bfloat16\n",
      "Using xformers cross attention\n",
      "****** User settings have been changed to be stored on the server instead of browser storage. ******\n",
      "****** For multi-user setups add the --multi-user CLI argument to enable multiple user profiles. ******\n",
      "\n",
      "ComfyUI finished loading, trying to launch localtunnel (if it gets stuck here localtunnel is having issues)\n",
      "\n",
      "The password/enpoint ip for localtunnel is: 118.178.240.30\n",
      "your url is: https://odd-peas-brush.loca.lt\n",
      "^C\n",
      "\n",
      "Stopped server\n"
     ]
    }
   ],
   "source": [
    "!npm install -g localtunnel\n",
    "%cd /mnt/workspace/ComfyUI\n",
    "import subprocess\n",
    "import threading\n",
    "import time\n",
    "import socket\n",
    "import urllib.request\n",
    "\n",
    "def iframe_thread(port):\n",
    "  while True:\n",
    "      time.sleep(0.5)\n",
    "      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "      result = sock.connect_ex(('127.0.0.1', port))\n",
    "      if result == 0:\n",
    "        break\n",
    "      sock.close()\n",
    "  print(\"\\nComfyUI finished loading, trying to launch localtunnel (if it gets stuck here localtunnel is having issues)\\n\")\n",
    "\n",
    "  print(\"The password/enpoint ip for localtunnel is:\", urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip(\"\\n\"))\n",
    "  p = subprocess.Popen([\"lt\", \"--port\", \"{}\".format(port)], stdout=subprocess.PIPE)\n",
    "  for line in p.stdout:\n",
    "    print(line.decode(), end='')\n",
    "\n",
    "\n",
    "threading.Thread(target=iframe_thread, daemon=True, args=(8188,)).start()\n",
    "\n",
    "!python main.py --dont-print-server"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "provenance": []
  },
  "gpuClass": "standard",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
